package com.example.function_interface.function;

import com.example.function_interface.User;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * 接受一个输入参数，返回一个结果。
 */
public class FunctionTest {

    @Test
    public void myTest() throws Exception{
        List<User> userList = new ArrayList<>();
        User user1 = new User(1, "张三", 12, false, 23.81);
        User user2 = new User(2, "李四", 22, false, 200);
        User user3 = new User(3, "王五", 32, true, 123.28);
        User user4 = new User(4, "郑静", 2, true, 0);
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);

        Function<User, String> function = new Function() {
            @Override
            public Object apply(Object o) {
                User user = (User) o;
                return user.getName();
            }
        };
        Function<Object, String> function1 = function.compose(obj -> {
            Integer id = (Integer) obj;
            return userList.get(id-1);
        });
        Function<User, Integer> function2 = function.andThen(name -> userList.stream().filter(user -> user.getName().equals(name)).findFirst().get().getAge());

        System.out.println(function1.apply(1));
        System.out.println(function2.apply(user1));
    }
}
